package com.example.springboot.controller;

import com.example.springboot.Service.IBorrowService;
import com.example.springboot.common.Result;
import com.example.springboot.controller.request.BorrowPageRequest;
import com.example.springboot.entity.Borrow;
import com.example.springboot.entity.Retur;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/borrow")
public class BorrowController {
    @Autowired
    IBorrowService iBorrowService;

    //四种注解表示，新增查询，删除，修改
    @PostMapping("/save")
    //@RequestBody 把Jason转换成User对象
    public Result save(@RequestBody Borrow borrow) {
        iBorrowService.save(borrow);
        return Result.success();
    }

    @PutMapping("/update")
    //@RequestBody 把Jason转换成User对象
    public Result update(@RequestBody Borrow borrow) {
        iBorrowService.update(borrow);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Borrow borrow = iBorrowService.getById(id);
        return Result.success(borrow);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        iBorrowService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        List<Borrow> users = iBorrowService.list();
        return Result.success(users);
    }

    @GetMapping("/page")
    public Result page(BorrowPageRequest borrowPageRequest) {
        return Result.success(iBorrowService.page(borrowPageRequest));
    }
    @PostMapping("/saveRetur")
    //@RequestBody 把Jason转换成User对象
    public Result saveRetur(@RequestBody Retur retur) {
        iBorrowService.saveRetur(retur);
        return Result.success();
    }
    @GetMapping("/pageRetur")
    public Result pageRetur(BorrowPageRequest borrowPageRequest) {
        return Result.success(iBorrowService.pageRetur(borrowPageRequest));
    }
    @DeleteMapping("/deleteRetur/{id}")
    public Result deleteRetur(@PathVariable Integer id) {
        iBorrowService.deleteReturById(id);
        return Result.success();
    }
//week month month2 month3
    @GetMapping("/lineCharts/{timeRange}")
    public Result lineCharts(@PathVariable String timeRange) {
        return Result.success(iBorrowService.getCountByTimeRange(timeRange));
    }
}
